<?php
/**
 * @Author:      ZH
 * @DateTime:    2016-2-16 15:32:48
 * @Description: 用于 pms 系统用户管理
 */
class PMSUserController extends BaseController {
    /*
     *  登陆
     * */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                'username' => 'required',
                'password' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("pms_error.login_args_error_210001");
                break;
            }
            //传入参数
            $username = Input::get('username');
            $password = SCUser::make(Input::get('password'));
            //验证密码
            $pms_user = PMSUser::findUserByUP($username,$password);
            if(!is_null($pms_user)){
                if($pms_user->status!=1){
                    $response = Lang::get("pms_error.login_user_status_error_210002");
                    break;
                }

                //删除 pms 用户 token
                try {
                    $old_token = PMSUserToken::where('pms_user_id',$pms_user->id)->first();
                    PMSUserToken::where('pms_user_id',$pms_user->id)->delete();
                    $pms_token = new PMSUserToken;
                    $pms_token->pms_user_id = $pms_user->id;
                    $pms_token->token = md5($username.time().rand(0,100));
                    $pms_token->created_at = Common::num2DateTime(time());
                    $pms_token->out_time_at = Common::num2DateTime(time() + Config::get("scs_user.token_time_out"));
                    $pms_token->save();
                    $new_token = PMSUserToken::where('pms_user_id',$pms_user->id)->first();
                    PmsOpLog::write('user',0,'login','用户:' . $pms_user->id . ' 登录','token:' . ($old_token?json_encode($old_token):null),'token:' . json_encode($new_token),0);
                } catch (Exception $e) {
                    Log::error("error", array("system" => "scs" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("pms_error.login_create_token_error_210004");
                    break;
                }
                if(isset($pms_token)) {
                    $response['result'] = ['name' => $pms_user->real_name, 'token' => $pms_token->token, 'expired' => $pms_token->out_time_at];
                    break;
                }
            }else{
                $response = Lang::get("pms_error.login_name_pwd_error_210003");
                break;
            }
        } while (false);
        Log::info("info", array("system" => "pms" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      ZH
     * @DateTime:    2016-2-16 19:04:14
     * @Description:  用户登出
     */
    public function logout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("pms_error.login_token_error_210005");
                break;
            }
            //传入参数
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $pms_user = $res['user'];
            try{
                $old_token = PMSUserToken::where('pms_user_id',$pms_user->id)->first();
                PMSUserToken::where('pms_user_id',$pms_user->id)->delete();
                PmsOpLog::write('user',0,'logout','用户:' . $pms_user->id . ' 登出','token:' . ($old_token?json_encode($old_token):null),'token:' . null,0);
            } catch (Exception $e) {
                Log::error("error", array("system" => "pms" ,"event" => "logout", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("pms_error.user_token_clean_error_210007");
                break;
            }
        } while (false);
        Log::info("info", array("system" => "pms" ,"event" => "logout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


}